<?php

namespace app\admin\controller;

use app\common\Error;
use think\facade\Request;
use app\common\Common;
use app\admin\service\PartnerService;

// 后台
class Partner extends Base
{
    private $service;

    public function __construct(PartnerService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    /**
     * 渲染列表模版
     */
    public function index()
    {
        return view();
    }

    /**
     * 渲染列表模版
     */
    public function info()
    {
        return view();
    }

    // 列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getPartnerList($params);

        return Common::success($data);
    }


    // 列表
    public function type()
    {
        $partner_type = config('app.partner_type', 'json');


        return Common::success($partner_type);
    }

    // 详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Admin::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getPartnerDetail($params['id']);

        return Common::success($data);
    }

    /**
     * 添加
     */
    public function add()
    {
        $params = Request::param();


        $result = $this->service->editPartner($params);

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 修改
    public function edit()
    {
        $params = Request::param();



        $result = $this->service->editPartner($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    /**
     * 删除
     *
     * @param int $id
     * @return json
     * @author zhixiang0215@126.com
     */
    public function delete()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Admin::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deletePartner($params['id']);

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::DELETE_FAIL;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 下拉列表
    public function select()
    {
        $data = $this->service->getPartnerSelectList();
        return Common::success($data);
    }

    // 下拉列表
    public function selectrule()
    {
        $data = $this->service->getruleSelectList();
        return Common::success($data);
    }

    /**
     * 头像上传
     */
    public function upload()
    {
        $base_path = $_SERVER['DOCUMENT_ROOT'] . "/uploads/images/"; // 接收文件目录
        if (!file_exists($base_path)) {
            mkdir($base_path, 0777, true);
        }
        $fileName = $_FILES['file']['name'];
        $target_path = $base_path . $fileName;
        $path = '/uploads/images/' . $fileName;
        if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
            $array = array(
                "code" => 1,
                "filename" => $fileName,
                "message" => $path
            );
            echo json_encode($array);
        } else {
            $array = array(
                "code" => 0,
                "filename" => "",
                "message" => "There was an error uploading the file, please try again!" . $_FILES['file']['error']);
            echo json_encode($array);
        }
    }
}